package jp.sourceforge.akjrcp.ffmpeg;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//TODO:update akj betas.
public class ProgressProcessMonitor {
	Log log=LogFactory.getLog(ProgressProcessMonitor.class);
	Process process;
	boolean monitor;

	private boolean monitorInput=true;
	private boolean monitorError=true;
	
	private ProgressReadListener progressReadListener;
	
	public ProgressProcessMonitor(Process process){
		this.process=process;
	
	}
	

	public ProgressReadListener getProgressReadListener() {
		return progressReadListener;
	}

	public void setProgressReadListener(ProgressReadListener progressReadListener) {
		this.progressReadListener = progressReadListener;
	}
	
	public void startMonitor(){
		monitor=true;
		if(monitorError)
			new ErrorMonitor().start();
		if(monitorInput)
			new InputMonitor().start();
	}
	InputStream inputReader,errorReader;
	public class ErrorMonitor extends Thread{
		public void run(){
			
			errorReader=process.getErrorStream();
			int b;
			StringBuffer buffer=new StringBuffer();
			try {
				while((b=errorReader.read())!=-1){
					if((char)b=='\n'){
					//	log.infon");
						String error=buffer.toString();
						if(FFMpegUtils.isProgressString(error)){
							if(progressReadListener!=null){
								progressReadListener.progress(error);
							}
						}
					buffer=new StringBuffer();
					}else if((char)b=='\r'){
						String error=buffer.toString();
						if(FFMpegUtils.isProgressString(error)){
							if(progressReadListener!=null){
								progressReadListener.progress(error);
							}
						}
						buffer=new StringBuffer();
						//log.infor");
					}else{
						buffer.append((char)b);
					}
					
				}
				errorReader.close();
				if(buffer.length()>0){
					String error=buffer.toString();
					if(FFMpegUtils.isProgressString(error)){
						if(progressReadListener!=null){
							progressReadListener.progress(error);
						}
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				
					try {
						errorReader.close();
						inputReader.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				
				e.printStackTrace();
			}
		}
	}
	
	public class InputMonitor extends Thread{
		public void run(){
			inputReader=process.getInputStream();
			byte tmp[]=new byte[10240];
			BufferedInputStream binput=new BufferedInputStream(inputReader);
			int b;
			try {
				while((b=binput.read(tmp))!=-1){
					
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
	
	/*
	BufferedReader inputReader,errorReader;
	
	public class ErrorMonitor extends Thread{
		public void run(){
			InputStream input=process.getErrorStream();
			
			inputReader=new BufferedReader(new InputStreamReader(input));
			String line2;
			try {
				while((line2=inputReader.readLine())!=null){
					printer.printError(line2+"\n");
					//Akj_betasPlugin.getDefault().addLog();
				}
				inputReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public class InputMonitor extends Thread{
		public void run(){
			InputStream input=process.getInputStream();
			
			errorReader=new BufferedReader(new InputStreamReader(input));
			String line2;
			try {
				while((line2=errorReader.readLine())!=null){
					printer.printOutput(line2+"\n");
				}
				errorReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}*/

	public void stop() {
		try {
			inputReader.close();
			errorReader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public boolean isMonitorInput() {
		return monitorInput;
	}
	public void setMonitorInput(boolean monitorInput) {
		this.monitorInput = monitorInput;
	}
	public boolean isMonitorError() {
		return monitorError;
	}
	public void setMonitorError(boolean monitorError) {
		this.monitorError = monitorError;
	}
}